/**
 * Description：理化或计量备注 hooks
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer：朱楠
 * date: 2024.9.26
 */
const { ref, reactive, nextTick } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage, ElMessageBox } = ElementPlus

export function useCheckEntrust() {
  const baseAPI = '../../../Ashx/ContainerCheckVue3PageHandler.ashx';

  const checkEntrustTableRef = ref(null);//表格ref

  const checkEntrustObj = reactive({
    dialogVisible: false,
    tabname: 'first',
    dataList: []
  });

  // 初始化数据
  async function initCheckEntrustDialogData(selectionRows) {
    checkEntrustObj.dialogVisible = true;
    checkEntrustObj.dataList = selectionRows;
    await nextTick(() => {
      checkEntrustTableRef.value.toggleAllSelection();
    });
  }

  // 【提交】按钮点击
  async function handleCheckEntrustSubmitClick() {
    const selections = checkEntrustTableRef.value.getSelectionRows();
    if (selections.length === 0) {
      return ElMessage({ type: 'error', message: `请选择流水卡`, grouping: true });;
    }
    ElMessageBox.confirm(`你确认要提交吗?`, '确认', { type: 'warning' }).then(async () => {
      const { data, status: { msg } } = await useFetch({
        url: `${baseAPI}?actions=saveCheckEntrust`,
        data: { type: 'saveCheckEntrust', postData: selections }
      });
      if (data.value === true) {
        ElMessage({ type: 'success', message: `保存成功`, grouping: true });
        checkEntrustObj.dialogVisible = false;
      } else {
        ElMessage({ type: 'error', message: msg, grouping: true });
      }
    }).catch(() => {
      ElMessage({ type: 'info', message: '已取消' });
    });
  }

  return {
    checkEntrustObj,
    checkEntrustTableRef,
    initCheckEntrustDialogData,
    handleCheckEntrustSubmitClick,
  };
}